package com.atguigu.exam.service.impl;

import com.atguigu.exam.entity.Notice;
import com.atguigu.exam.mapper.NoticeMapper;
import com.atguigu.exam.service.NoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Override
    public List<Notice> getAllNotices() {
        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Notice::getPriority, Notice::getCreateTime);
        return this.list(wrapper);
    }

    @Override
    public List<Notice> getActiveNotices() {
        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Notice::getPriority, Notice::getCreateTime)
                .eq(Notice::getIsActive, true);
        return this.list(wrapper);
    }

    @Override
    public List<Notice> getLatestNotices(Integer limit) {
        return baseMapper.selectLatestNotice(limit);
    }

    @Override
    public Boolean addNotice(Notice notice) {
        return this.save(notice);
    }

    @Override
    public Boolean updateNotice(Notice notice) {
        notice.setUpdateTime(new Date());
        return this.updateById(notice);
    }

    @Override
    public Boolean toggleNoticeStatus(Long id, Boolean isActive) {
        LambdaUpdateWrapper<Notice> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Notice::getIsActive, isActive)
                .set(Notice::getUpdateTime, new Date())
                .eq(Notice::getId, id);
        return this.update(wrapper);
    }

    @Override
    public Boolean deleteById(Long id) {
        return this.removeById(id);
    }


}
